using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using DG.Tweening;
using Object = System.Object;
using Random = UnityEngine.Random;

public class CardDeck : MonoBehaviour
{
   public CardManager cardManager;

   public CardLayoutManager cardLayoutManager;
   //抽牌堆
   private List<CardDataDetails> drawDeck = new();
   //弃牌堆
   private List<CardDataDetails> discardDeck = new();
   //每回合的手牌
   private List<Card> handCardObjectList = new();

   public Vector3 deckPosition;
   [Header("广播事件")]
   public IntEventSO drawDeckAmountEvent;
   public IntEventSO discardDeckAmountEvent;

   //测试用
   private void Start()
   {
      InitializeDeck();
   }

   public void InitializeDeck()
   {
      drawDeck.Clear();
      //初始化抽牌堆
      foreach (var entry in cardManager.newGameLibrary)
      {
         for (int i = 0; i < entry.amount; i++)
         {
            drawDeck.Add(entry.cardData);
         }
      }
      //洗牌/更新抽牌堆or弃牌堆显示的数字
      ShuffleDeck();
   }

   [ContextMenu("TestDraw")]
   public void TestDraw()
   {
      DrawCard(1);
   }
   
   //新回合开始，抽牌
   public void NewTurnDrawCard()
   {
      if (cardManager.gameLevel == GameLevel.Primary)
      {
         DrawCard(3);
      }
      else if (cardManager.gameLevel == GameLevel.Intermdiate)
      {
         DrawCard(4);
      }
      else if (cardManager.gameLevel == GameLevel.Senior)
      {
         DrawCard(5);
      }
      
   }

   //抽牌
   public void DrawCard(int amount)
   {
      for (int i = 0; i < amount; i++)
      {
         CardDataDetails currentCardData = drawDeck[0];
         drawDeck.RemoveAt(0);
         if(drawDeck.Count == 0)
         {
            //如果抽牌堆为空，将弃牌堆洗牌放入抽牌堆
            foreach (var item in discardDeck)
            {
               drawDeck.Add(item);
            }
            ShuffleDeck();

            //洗牌/更新抽牌堆or弃牌堆显示的数字
         }
         
         //更新UI显示数量
         drawDeckAmountEvent.RaisEvent(drawDeck.Count,this);
         
         var card = cardManager.GetCardObject().GetComponent<Card>();
         //初始化卡牌
         card.Init(currentCardData);
         card.transform.position = deckPosition;
         //玩家卡牌库添加卡牌
         handCardObjectList.Add(card);
         //设置延迟时间
         var delay = i * 0.2f;
         //设置卡牌位置
         SetCardLayout(delay);
      }
   }

   private void SetCardLayout(float delay)
   {
      for (int i = 0; i < handCardObjectList.Count; i++)
      {
         var currentCard = handCardObjectList[i];
         CardTransform cardTransform = cardLayoutManager.GetCardTransform(i, handCardObjectList.Count);
         //currentCard.transform.SetPositionAndRotation(cardTransform.pos,cardTransform.rotation);
         
         
         currentCard.UpdateCardState();
         
         currentCard.isAnimating = true;
         
         //设置卡牌动画
         currentCard.transform.DOMove(cardTransform.pos, 0.2f).SetDelay(delay).onComplete = () =>
         {
            currentCard.transform.DOScale(Vector3.one, 0.5f).onComplete = () =>currentCard.isAnimating =false;
            currentCard.transform.DORotateQuaternion(cardTransform.rotation, 0.5f); 
         };
         //设置卡牌排序
         currentCard.GetComponent<SortingGroup>().sortingOrder = i;
         //保存卡牌初始数据
         currentCard.UndatePositionAndRotation(cardTransform.pos, cardTransform.rotation);
      }
   }

   /// <summary>
   /// 洗牌
   /// </summary>
   private void ShuffleDeck()
   {
      discardDeck.Clear();
      //更新UI显示数量
      drawDeckAmountEvent.RaisEvent(drawDeck.Count,this);
      discardDeckAmountEvent.RaisEvent(discardDeck.Count,this);
      
      //洗牌
      for (int i = 0; i < drawDeck.Count; i++)
      {
         CardDataDetails temp = drawDeck[i];
         int randomIndex = Random.Range(i, drawDeck.Count);
         drawDeck[i] = drawDeck[randomIndex];
         drawDeck[randomIndex] = temp;
      }
   }
   /// <summary>
   /// 弃牌
   /// </summary>
   /// <param name="card"></param>
   public void DiscardCard(Object obj)
   {
      Card card = obj as Card;
      
      handCardObjectList.Remove(card);
      discardDeck.Add(card.cardDataSO);
      
      cardManager.DiscardCardObject(card.gameObject);
      
      //更新UI显示数量
      discardDeckAmountEvent.RaisEvent(discardDeck.Count,this);
      
      SetCardLayout(0);
   }
   
   /// <summary>
   /// 回合结束后清空手牌
   /// </summary>
   public void OnPlayerTurnEnd()
   {
      foreach (var card in handCardObjectList)
      {
         discardDeck.Add(card.cardDataSO);
         cardManager.DiscardCardObject(card.gameObject);
      }
      //更新UI显示数量
      discardDeckAmountEvent.RaisEvent(discardDeck.Count,this);
      //清空手牌
      handCardObjectList.Clear();
   }

   public void ResetAllCards(Object obj)
   {
      foreach (var card in handCardObjectList)
      {
         cardManager.DiscardCardObject(card.gameObject);
      }
      handCardObjectList.Clear();
      InitializeDeck();
   }
}
